package cn.iocoder.yudao.module.member.controller.admin.deliverypersoninfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.deliverypersoninfo.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.deliverypersoninfo.DeliveryPersonInfoDO;
import cn.iocoder.yudao.module.member.service.deliverypersoninfo.DeliveryPersonInfoService;

@Tag(name = "管理后台 - 配送员信息扩展")
@RestController
@RequestMapping("/member/delivery-person-info")
@Validated
public class DeliveryPersonInfoController {

    @Resource
    private DeliveryPersonInfoService deliveryPersonInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建配送员信息扩展")
    @PreAuthorize("@ss.hasPermission('member:delivery-person-info:create')")
    public CommonResult<Long> createDeliveryPersonInfo(@Valid @RequestBody DeliveryPersonInfoSaveReqVO createReqVO) {
        return success(deliveryPersonInfoService.createDeliveryPersonInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送员信息扩展")
    @PreAuthorize("@ss.hasPermission('member:delivery-person-info:update')")
    public CommonResult<Boolean> updateDeliveryPersonInfo(@Valid @RequestBody DeliveryPersonInfoSaveReqVO updateReqVO) {
        deliveryPersonInfoService.updateDeliveryPersonInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配送员信息扩展")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:delivery-person-info:delete')")
    public CommonResult<Boolean> deleteDeliveryPersonInfo(@RequestParam("id") Long id) {
        deliveryPersonInfoService.deleteDeliveryPersonInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配送员信息扩展")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:delivery-person-info:query')")
    public CommonResult<DeliveryPersonInfoRespVO> getDeliveryPersonInfo(@RequestParam("id") Long id) {
        DeliveryPersonInfoDO deliveryPersonInfo = deliveryPersonInfoService.getDeliveryPersonInfo(id);
        return success(BeanUtils.toBean(deliveryPersonInfo, DeliveryPersonInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配送员信息扩展分页")
    @PreAuthorize("@ss.hasPermission('member:delivery-person-info:query')")
    public CommonResult<PageResult<DeliveryPersonInfoRespVO>> getDeliveryPersonInfoPage(@Valid DeliveryPersonInfoPageReqVO pageReqVO) {
        PageResult<DeliveryPersonInfoDO> pageResult = deliveryPersonInfoService.getDeliveryPersonInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryPersonInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配送员信息扩展 Excel")
    @PreAuthorize("@ss.hasPermission('member:delivery-person-info:export')")
    @OperateLog(type = EXPORT)
    public void exportDeliveryPersonInfoExcel(@Valid DeliveryPersonInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryPersonInfoDO> list = deliveryPersonInfoService.getDeliveryPersonInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配送员信息扩展.xls", "数据", DeliveryPersonInfoRespVO.class,
                        BeanUtils.toBean(list, DeliveryPersonInfoRespVO.class));
    }

}